package io.xccit.aischool.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.xccit.aischool.entity.LoginForm;
import io.xccit.aischool.entity.Student;
import io.xccit.aischool.mapper.IStudentMapper;
import io.xccit.aischool.service.IStudentService;
import io.xccit.aischool.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author CH_ywx
 * @date 2023-06-06
 * @description
 */
@Service("studentService")
@Transactional
public class StudentServiceImpl extends ServiceImpl<IStudentMapper, Student> implements IStudentService {

    @Autowired
    private IStudentMapper studentMapper;
    @Override
    public Student login(LoginForm loginForm) {
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.eq("name",loginForm.getUsername());
        wrapper.eq("password", MD5.encrypt(loginForm.getPassword()));
        return studentMapper.selectOne(wrapper);
    }


    @Override
    public Page<Student> getStudentByOpr(Page<Student> studentPage, String clazzName) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(clazzName)){
            queryWrapper.like("clazz_name",clazzName);
        }
        return studentMapper.selectPage(studentPage,queryWrapper);
    }

    @Override
    public Student getUserByID(Long userId) {
        return studentMapper.selectById(userId);
    }
}
